/*
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 */
function onDeleteFeatured(url,featuredId,product,name,unit_cost,id,symbol,position) {
	var isOK = -1;
	
	if(!confirm('Are you sure you want to delete the Featured with Id:' + featuredId)) {
		return false;
	}
	
	new Ajax.Request(url, {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=deletefeatured&id=' + featuredId+'&position='+position,
	  onSuccess: function(transport) 
  		{
			var response = transport.responseText || "";
				var pos=response.indexOf('<!');
				content=response.substring(0,pos);
		    	//alert(content);

			obj = content.evalJSON();

    		if(obj == null) {
    			$('ajaxResponse1').innerHTML = 'Something wrong! Reponsed string is: ' + response;
    		}
    		
    		isOK = obj.code;
		
    		$('ajaxResponse1').update(obj.message);
		}
	});

	$('ajaxResponse1').style.display = 'inherit';
	Fat.fade_element('ajaxResponse1', null, 1500, null, null);

	if(isOK == 0) {
		rowID = featuredId;
		Fat.fade_element(rowID, null, 1000, "#f22", null);
		setTimeout("$(rowID).remove()", 1000);
	}				
}

function onAddNewFeatured(frmID, msgDiv, cattable,product) {
	var newId = '';
	
	$(frmID).request( {
			asynchronous: false,
			parameters: $(frmID).serialize(true),
	  		onComplete: function(transport)
	  		{ 
			  	var response = transport.responseText || "";
				var pos=response.indexOf('<!');
				content=response.substring(0,pos);
		    	//alert(content);
		    	
		    	$(msgDiv).innerHTML = '';
		    	//alert(response);
		    	if(content != "") {
		    		obj = content.evalJSON();
		    		if(obj == null) {
		    			$(msgDiv).update('Something wrong! Reponsed string is: ' + response);
		    		}
		    		
			    	if(msgDiv != null)
				    	$(msgDiv).update(obj.message);
				    if(obj.ID > -1) {
					    newId = obj.ID;
					    	if(cattable != null && $(cattable) != null) {
								sClassName = 'newrow';
								
						    		//repair new row used to add to table
							    	str = '<tr id="' + obj.ID + '" class="' + sClassName + '">';
									str += '<th scope="row" style="text-align: center">'
											+ obj.ID + '</th>';
									str += '<td align="left">' + obj.product_name + '</td>';
									str += '<td align="right"><s>'+ obj.product_price + obj.symbol_left +'</s>&nbsp;'+ obj.featured_new_products_price + obj.symbol_left +'</td>';

									if(obj.status ==1) {
										str += '<td align="center">' + 'x' + '</td>';
									} else {
										str += '&nbsp;' ;
									}
									str += '<td><a href="?module=BgcmsWebshop&sub1=featured&act=editfeatured&id=' + obj.ID + '" class="edit">Edit</a></td>';
									str += '<td><a onclick="onDeleteFeatured(\'' + 
									$(frmID).action + '\',\'' + obj.ID + '\',\'' + product + '\',\'' + obj.product_name + '\',\'' + obj.product_price + '\',\'' + obj.product_id + '\',\'' + obj.symbol_left + '\')" class="delete">Delete</a></td>';

									str += '</tr>';
								
									$(cattable).innerHTML += str;
									
					    	}
							
		    		}
		    	}
	  		}
		});

	$(msgDiv).style.display = 'block';
	Fat.fade_element(msgDiv, null, 1500, null, null);
	if(newId != '') {
		Fat.fade_element(newId);
		$(frmID).reset();
	}
	//revent submit page
	return false;
}

function OnGetImage(url, productID, frameID) {
	var frame= document.getElementById(frameID);
	new Ajax.Request(url, {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=getimage&id=' + productID.value,
	  onSuccess: function(transport) {
			var response = transport.responseText || "";
				var pos=response.indexOf('<!');
				content=response.substring(0,pos);
		    	//alert(content);
				frame.innerHTML="";	
				obj = content.evalJSON();
				var image = document.createElement("img");
					image.src = obj.image;
					frame.appendChild(image);
		}
	});
}